﻿/**
 *
 * cs1237.c
 *
 *  Created on: 2019-11-19
 *      Author: chenshisheng
 */

#include "cs1237.h"
#include "hw.h"
#include "sspi.h"

void CS1237_Init(const CS1237_Bus_t *bus)
{
    CS1237_PowerDownCmd(bus, false);
}

bool CS1237_IsNewDataReady(const CS1237_Bus_t *bus)
{
    return HW_GET(&bus->dout) ? false : true;
}

int8_t CS1237_Read(const CS1237_Bus_t *bus, int32_t *adc)
{
    uint8_t d[3];

    SSPI_Recv(&bus->sclk, &bus->dout, d, sizeof(d));
    SSPI_Clk(&bus->sclk, 3);
    *adc = (((int32_t)d[0]) << 16) | (((int32_t)d[1]) << 8) | d[2];
    if(d[0] & 0x80)
    {
        *adc |= 0xff000000;
    }

    return 0;
}

void CS1237_PowerDownCmd(const CS1237_Bus_t *bus, bool state)
{
    if(state)
    {
        HW_SET(&bus->sclk);
    }
    else
    {
        HW_CLR(&bus->sclk);
    }
}
